package com.ostrich.a2104gao5.widget;

import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;

import java.text.SimpleDateFormat;

import androidx.annotation.Nullable;


public class ProgressView extends View {
    private Paint mPaint;
    private int defaultColor = Color.GRAY;
    private int radiusColor = Color.GREEN;
    private int radius = 300;
    private float progress; //扫描进度
    private int timestamp;//10分钟

    private String timeText = "" ;//"11：03：57"
    private Rect mBound;

    private String infoText  = "" ;//"站岗中"
    private Rect infoBound;


    private String time   = "";//"08:00"
    private Rect timeBound;








    private ValueAnimator progressAnimator;
    private ValueAnimator timeAnimator;
    public ProgressView(Context context) {
        super(context);
        initPaint();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }

    /**
     * 初始化画笔
     */
    private void initPaint(){
        mBound = new Rect();
        infoBound = new Rect();
        timeBound = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setAntiAlias(true);//抗锯齿
        mPaint.setDither(true);//防抖动

        mPaint.getTextBounds(timeText,0,timeText.length(),mBound);//mBound文字区域大小
        mPaint.getTextBounds(infoText,0,infoText.length(),infoBound);//mBound文字区域大小
        mPaint.getTextBounds(time,0,time.length(),timeBound);//mBound文字区域大小

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //宽度
        int width_mode = MeasureSpec.getMode(widthMeasureSpec);
        int width_size = MeasureSpec.getSize(widthMeasureSpec);

        if(width_mode == MeasureSpec.EXACTLY){//精确模式:100dp或者MATCH_PARENT
            width_size = width_size;
        }else if(width_mode == MeasureSpec.AT_MOST){//最大值模式:wrap_content
            width_size = (int) (radius*2);
        }else if(width_mode == MeasureSpec.UNSPECIFIED){//不确定测量模式：listview recyclerview
        }

        //高度
        int height_mode = MeasureSpec.getMode(heightMeasureSpec);
        int height_size = MeasureSpec.getSize(heightMeasureSpec);
        if(height_mode == MeasureSpec.EXACTLY){//精确模式:100dp或者MATCH_PARENT
            height_size = height_size;
        }else if(height_mode == MeasureSpec.AT_MOST){//最大值模式:wrap_content
            height_size = (int) (radius*2);
        }else if(height_mode == MeasureSpec.UNSPECIFIED){//不确定测量模式：listview recyclerview
        }
        //注意！！！！必须设置控件的大小
        setMeasuredDimension(width_size,height_size);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(defaultColor);
        canvas.drawCircle(width/2,height/2,radius,mPaint);
        mPaint.setColor(radiusColor);
        RectF rectF = new RectF(width / 2 - radius, height / 2 - radius, width / 2 + radius, height / 2 + radius);
        canvas.drawArc(rectF,-90,progress,false,mPaint);

        //绘制文字:宽度和高度有偏移量
        mPaint.setStyle(Paint.Style.FILL);
        {
            mPaint.setTextSize(60);//文字大小
            int x = (int) (width / 2 - mPaint.measureText(timeText) / 2);
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
            float textHeight = Math.abs((fontMetrics.bottom - fontMetrics.top));
            int y = (int) (height / 2 + textHeight/2);
            canvas.drawText(timeText,x,y,mPaint);
        }

        {
            mPaint.setTextSize(30);//文字大小
            int x = (int) (width / 2 - mPaint.measureText(infoText) / 2);
            int y = (int) (height / 2 - radius/2);
            canvas.drawText(infoText,x,y,mPaint);
        }
        {

            mPaint.setTextSize(30);//文字大小
            int x = (int) (width / 2 -  mPaint.measureText("将在 今天的"+time+" 关闭") / 2);
            int y = (int) (height / 2 + radius/2);
            canvas.drawText("将在 ",x,y,mPaint);
        }

        {

            mPaint.setTextSize(30);//文字大小
            mPaint.setColor(Color.BLACK);
            int x = (int) (width / 2 - mPaint.measureText("今天的"+time) / 2);
            int y = (int) (height / 2 + radius/2);
            canvas.drawText("今天的"+time,x,y,mPaint);
        }
        {

            mPaint.setTextSize(30);//文字大小
            mPaint.setColor(radiusColor);
            int x = (int) (width / 2 + mPaint.measureText("今天的"+time) / 2 );
            int y = (int) (height / 2 + radius/2);
            canvas.drawText(" 关闭",x,y,mPaint);
        }


    }

    //开启动画
    public void startAnimation(){

        {
            progressAnimator = ValueAnimator.ofFloat(0, 360);
            progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    progress = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
            progressAnimator.setDuration(timestamp);
            progressAnimator.start();
        }
        {
            timeAnimator= ValueAnimator.ofInt(timestamp, 0);
            timeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int time = (int) animation.getAnimatedValue();
                    int hour = time / 1000 / 60 / 60;
                    int minute = time / 1000 / 60 ;
                    int second = time / 1000 ;
                    StringBuilder stringBuilder = new StringBuilder();
                    if(hour < 10){
                        stringBuilder.append("0");
                    }
                    stringBuilder.append(hour);
                    stringBuilder.append("：");
                    if(minute%60 < 10){
                        stringBuilder.append("0");
                    }
                    stringBuilder.append(minute%60);
                    stringBuilder.append("：");
                    if(second%60 < 10){
                        stringBuilder.append("0");
                    }
                    stringBuilder.append(second%60);


                    timeText = stringBuilder.toString();
                    invalidate();

                }
            });
            timeAnimator.setDuration(timestamp);
            timeAnimator.start();
        }


        time = getCurrentTimeStamp();


    }

    public String getCurrentTimeStamp(){
        long l = System.currentTimeMillis() + timestamp ;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        String format = simpleDateFormat.format(l);
        return format;

    }

    public void stopAnimation(){
        if(timeAnimator != null){
            timeAnimator.cancel();

        }
        if(progressAnimator != null){
            progressAnimator.cancel();

        }
    }

    public ProgressView setRadius(int radius) {
        this.radius = radius;
        return this;
    }

    public ProgressView setProgress(float progress) {
        this.progress = progress;
        return this;
    }

    public ProgressView setTimestamp(int timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public ProgressView setInfoText(String infoText) {
        this.infoText = infoText;
        return this;
    }
}
